/**
 * 
 */
package org.seginfo.httpfuzzer.header;

import java.io.Serializable;

/**
 * @author Administrador
 *
 */
public abstract class CommonStrategy implements AttackStrategy, Serializable {

	/**
	 * @see org.seginfo.httpfuzzer.header.AttackStrategy#getDateAttack()
	 */
	public String getDateAttack() {
		return null;
	}

	/**
	 * Devuelve un string que representa un entero de tamaño <i>size</i>
	 */
	public String getIntegerAttack(int size) {
		StringBuffer buffer = new StringBuffer();
		for (int i=0; i<(int) (Math.random() * size);i++) {
			buffer.append((char) (48 + ((int) Math.random()*10)));
		}
		return buffer.toString();
	}

	/**
	 * Devuelve un string que representa un entero
	 */
	public String getIntegerAttack() {
		return getIntegerAttack((int) (Math.random() * 1000000000));
	}

	/**
	 * @see org.seginfo.httpfuzzer.header.AttackStrategy#getStringAttack()
	 */
	public String getStringAttack() {
		return getStringAttack(0, (int) (Math.random() * 1000000000));
	}
	/**
	 * @see org.seginfo.httpfuzzer.header.AttackStrategy#getStringAttack()
	 */
	public String getStringAttack(int minLength, int maxLength) {
		int size = (int) (Math.random() * maxLength) + minLength;
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < size; i++) {
			buffer.append((char) ((Math.random() * 26) + 65));
		}
		return buffer.toString();
	}

	/**
	 * @see org.seginfo.httpfuzzer.header.AttackStrategy#getFloatAttack()
	 */
	public String getFloatAttack() {
		double p = Math.random();
		
		/* Negativo con tama�o v�lido */
		if (p < 0.25) {
			return "-0.23";
		}
		
		/* Mayor cantidad de d�gitos */
		else if (p < 0.5) {
			return "0.1234567878";
		}
		
		/* N�meros mayores que 1 */
		else if (p < 0.75) {
			return "15.33";
		}
		
		/* n�meros negativos grandes */
		else {
			return "-73.4";
		}
	}

	/**
	 * Devuelve un string que representa un hexa
	 */
	public String getHexAttack(int minLength, int maxLength) {
		int size = (int) (Math.random() * maxLength) + minLength;
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < size; i++) {
			buffer.append(getRandomHex());
		}
		return buffer.toString();
	}

	/**
	 * Helper
	 * @return
	 */
	protected String getRandomHex() {
		int leftNibble = (int) (Math.random() * 15);
		int rightNibble = (int) (Math.random() * 15);

		StringBuffer buffer = new StringBuffer();
		buffer.append(this.intToChar(leftNibble));
		buffer.append(this.intToChar(rightNibble));
		return buffer.toString();
	}

	/**
	 * Helper
	 * @return
	 */
	protected char intToChar(int number) {
		if (number >= 0 && number <= 9) {
			return (char) (48 + number);
		}
		
		else if (number >= 10 && number <= 15) {
			return (char) (55 + number);
		}
		else {
			return '0';
		}
	}
}